package com.his.service.impl;

import com.his.common.Temp;
import com.his.entity.Application;
import com.his.entity.Bed;
import com.his.entity.Ward;
import com.his.mapper.BedMapper;
import com.his.service.ApplicationService;
import com.his.service.BedService;
import com.his.service.WardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;

/**
 * (Bed)表服务实现类
 *
 * @author makejava
 * @since 2020-10-23 18:05:59
 */
@Service("bedService")
public class BedServiceImpl implements BedService {
    @Resource
    private BedMapper bedMapper;

    @Autowired
    private ApplicationService applicationService;

    @Autowired
    private WardService wardService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Bed queryById(Integer id) {
        return this.bedMapper.queryById(id);
    }

    /**
     * 查询所有通过实体类属性
     *
     * @param bed 实例对象
     * @return 查询的结果
     */
    @Override
    public List<Bed> queryAll(Bed bed) {
        return this.bedMapper.queryAll(bed);
    }

    /**
     * 新增数据
     *
     * @param bed 实例对象
     * @return 实例对象
     */
    @Override
    public Bed insert(Bed bed) {
        this.bedMapper.insert(bed);
        return bed;
    }

    /**
     * 修改数据
     *
     * @param bed 实例对象
     * @return 实例对象
     */
    @Override
    public Bed update(Bed bed) {
        this.bedMapper.update(bed);
        return this.queryById(bed.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.bedMapper.deleteById(id) > 0;
    }

    @Override
    public List<Bed> queryByWardId(String wardId) {
        return bedMapper.queryByWardId(Integer.parseInt(wardId));
    }

    @Override
    public Integer insertBedByPatientId(Temp temp) {
        Application application = new Application();
        application.setId(temp.getApplicantInfo().getAppId());
        application.setBadId(temp.getBed().getId());
        application.setStatus(1);
        application.setInTime(new Date());
        //更新申请表信息
        applicationService.update(application);
        temp.getBed().setBedStatus("0");
        //更新病床信息
        bedMapper.update(temp.getBed());
        List<Bed> bedList = bedMapper.queryByWardId(temp.getBed().getWardId());
        int count = 0;
        for (Bed bed : bedList) {
            if (bed.getBedStatus().equals("0")) {
                count++;
                //若所有病床均满，则更新病房信息
                if (count == bedList.size()) {

                    Ward ward = wardService.queryById(bed.getWardId());
                    ward.setWardStatus("0");
                    wardService.update(ward);
                }
            }
        }
        return 20000;
    }

    @Override
    public Integer updateBedByPatientId(Temp temp) {
        Application application = new Application();
        application.setId(temp.getApplicantInfo().getAppId());
        application.setBadId(temp.getBed().getId());
        application.setStatus(1);
        application.setInTime(new Date());
        //更新申请表信息-改变病人的病床信息
        applicationService.update(application);
        temp.getBed().setBedStatus("0");
        //更新新病床信息
        bedMapper.update(temp.getBed());
        List<Bed> bedList = bedMapper.queryByWardId(temp.getBed().getWardId());
        int count = 0;
        for (Bed bed : bedList) {
            if (bed.getBedStatus().equals("0")) {
                count++;
                //若所有病床均满，则更新病房信息
                if (count == bedList.size()) {
                    Ward ward = wardService.queryById(bed.getWardId());
                    ward.setWardStatus("0");
                    wardService.update(ward);
                }
            }
        }
        //更新原病床的病床状态
        Integer oldBedId = temp.getApplicantInfo().getBedId();
        Bed bed = new Bed();
        bed.setId(oldBedId);
        bed.setBedStatus("1");
        bedMapper.update(bed);
        //查询所在原病床的病房状态
        bed = bedMapper.queryById(bed.getId());
        Ward ward = wardService.queryById(bed.getWardId());
        if (ward.getWardStatus().equals("0")) {
            ward.setWardStatus("1");
            wardService.update(ward);
        }
        return 20000;
    }
}
